package com.panshi.controller;

import com.panshi.pojo.User;
import com.panshi.service.impl.UserServiceImpl;
import com.panshi.utils.RedisUtil;
import io.swagger.annotations.ApiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

@Controller
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @Autowired
    RedisUtil redisUtil;

    @RequestMapping("/user/login")
    public String userLogin(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpSession session) {
        System.out.println("賬戶： " + username + "密碼：" + password);
        Integer flag = userService.getUser(username, password);
        System.out.println("登录 ======  " + flag);
        if (flag == null) {   //如果没有查出数据 表示失败直接返回去
            model.addAttribute("msg", "用戶名錯誤 || 密碼錯誤");
            return "index";
        }
        //成功
        session.setAttribute("loginUser", username);
        session.setAttribute("loginUid", flag);
        return "redirect:/dashboard";

    }

}
